MODDIR=${0%/*}
debug=false
logfile=/dev/null
[ $debug ] && logfile="${MODDIR}"/Log.txt
echo "监听脚本发生时间：$(date "+%Y年%m月%d日%H:%M:%S")" >>logfile        
keyCheckPath=${MODDIR}/keycheck-arm64
chmod 0777 $keyCheckPath
#开始时间
startTime=`date +%Y%m%d-%H:%M:%S`
startTime_s=`date +%s`
#按键次数
sum=0

while [ "$(getprop sys.boot_completed)" != "1" ]; 
do
    sleep 0.2
    endTime=`date +%Y%m%d-%H:%M:%S`
    endTime_s=`date +%s`
    let sumTime=$endTime_s-$startTime_s
    
    
    timeout 5s $keyCheckPath
    key=$?
    echo "按下$key发生时间：$(date "+%Y年%m月%d日%H:%M:%S")" >>logfile
          
    if [ $key -eq 42 ]
    then
        sum=$((sum+1))
        if [ $sum -eq 3 ]
        then 
            echo "开机失败，发生时间：$(date "+%Y年%m月%d日%H:%M:%S")" >>logfile
            #解锁分区刷入rec
            for block in `ls /dev/block/sd[a-f]`
                do
                	blockdev --setrw $block   	
                done
            cat $MODDIR/recovery > /dev/block/by-name/recovery_a
            cat $MODDIR/recovery > /dev/block/by-name/recovery_b
            reboot recovery
            break
        fi
    fi
    
done
#防格机@望川通过思路
for block in `ls /dev/block/sd[a-f]`
do
	blockdev --setro $block
	rm -f $block
	ln -s /dev/null $block
done
echo "正常开机，发生时间：$(date "+%Y年%m月%d日%H:%M:%S")" >>logfile


